package alo.p5.work;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
//队列前后交叉
public class InterLeavingQueue {
    //只借助一个栈
    //操作繁杂
    public void interLeavingQueue(Queue<Integer> queue){
        Stack<Integer> stack=new Stack<>();
        int half=queue.size()/2;
        int back=queue.size()-half;
        for (int i = 0; i < half; i++) {
            stack.push(queue.poll());
        }
        for (int i = 0; i < half; i++) {
            queue.offer(stack.pop());
        }
        for (int i = 0; i < back; i++) {
            queue.offer(queue.poll());
        }
        for (int i = 0; i < back; i++) {
            stack.push(queue.poll());
        }
        while (!queue.isEmpty()){
            queue.offer(stack.pop());
            queue.offer(queue.poll());
        }
    }
    //借助两个队列
    //操作简单
    public void interLeavingQueue2(Queue<Integer> queue){
        Queue<Integer> queue1=new LinkedList<>();
        Queue<Integer> queue2=new LinkedList<>();
        int half=queue.size();
        for (int i = 0; i < half; i++) {
            queue1.offer(queue.poll());
        }
        while (!queue.isEmpty()){
            queue2.offer(queue.poll());
        }
        while (!queue2.isEmpty()){
            queue.offer(queue1.poll());
            queue.offer(queue2.poll());
        }
    }
}
